//
//  XMSystemSettingController.m
//  XMWeibo
//
//  Created by chi on 14-9-2.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMSystemSettingController.h"
#import "XMCommonSetttingController.h"
#import "MBProgressHUD+XM.h"
#import "XMWeiboTool.h"
#import "XMWeiboAccount.h"

#define kLogoutBtnHeight (44.0)

@interface XMSystemSettingController ()

/** 退出登录视图 */
@property (nonatomic, strong) UIView *logoutView;

@end

@implementation XMSystemSettingController



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 添加退出按钮
    [self setupLogoutButton];
    
    // 设置属性
    self.navigationItem.title = @"设置";
    
    // 添加分组
    [self setupGroup1];
    [self setupGroup2];
    [self setupGroup3];
    [self setupGroup4];
}


- (void)setupLogoutButton
{
    _logoutView = [[UIView alloc]init];
    
    _logoutView.bounds = CGRectMake(0.0, 0.0, 0.0, kLogoutBtnHeight);
    
    UIButton *logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [logoutBtn setTitle:@"重置软件" forState:UIControlStateNormal];
    
    logoutBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    
    
    [logoutBtn addTarget:self action:@selector(clickLogout) forControlEvents:UIControlEventTouchUpInside];
    
    [logoutBtn setBackgroundImage:[UIImage stretchableImageWithName:@"common_button_red"] forState:UIControlStateNormal];
    [logoutBtn setBackgroundImage:[UIImage stretchableImageWithName:@"common_button_red_highlighted"] forState:UIControlStateHighlighted];
    CGFloat edge = 5.0;
    
    CGFloat viewW = self.tableView.bounds.size.width;
    
    logoutBtn.frame = CGRectMake(edge, 10, viewW - 2 * edge, kLogoutBtnHeight - edge);
    
    [_logoutView addSubview:logoutBtn];
}

- (void)clickLogout
{
    NSFileManager *mgr = [NSFileManager defaultManager];
    // 删除我的文档
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];;
    [mgr removeItemAtPath:path error:nil];
    
    // 删除缓存
    path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];;
    [mgr removeItemAtPath:path error:nil];
    
    [MBProgressHUD showSuccess:@"重置软件成功"];
}


#pragma mark - 添加分组

- (void)setupGroup1
{
    XMGroupInfo *group1 = [self addGroup];
    
    XMArrowSettingItem *acc = [XMArrowSettingItem arrowCellItemWithTitle:@"帐号管理" subTitle:nil icon:nil class:nil];
    
    group1.cellItems = @[acc];
}

- (void)setupGroup2
{
    XMGroupInfo *group2 = [self addGroup];
    
    XMArrowSettingItem *theme = [XMArrowSettingItem arrowCellItemWithTitle:@"主题背景" subTitle:nil icon:nil class:nil];
    
    group2.cellItems = @[theme];
}

- (void)setupGroup3
{
    XMGroupInfo *group3 = [self addGroup];
    
    XMArrowSettingItem *notify = [XMArrowSettingItem arrowCellItemWithTitle:@"通知和提醒" subTitle:nil icon:nil class:nil];
    
    
    XMArrowSettingItem *common = [XMArrowSettingItem arrowCellItemWithTitle:@"通用设置" subTitle:@"(只有此处可以跳转)" icon:nil class:[XMCommonSetttingController class]];
    
    XMArrowSettingItem *security = [XMArrowSettingItem arrowCellItemWithTitle:@"隐私与安全" subTitle:nil icon:nil class:nil];
    
    
    group3.cellItems = @[notify, common, security];
    
}

- (void)setupGroup4
{
    XMGroupInfo *group4 = [self addGroup];
    
    XMArrowSettingItem *suggestion = [XMArrowSettingItem arrowCellItemWithTitle:@"意见反馈" subTitle:nil icon:nil class:nil];
    
    XMArrowSettingItem *about = [XMArrowSettingItem arrowCellItemWithTitle:@"关于微博" subTitle:nil icon:nil class:nil];

    
    group4.cellItems = @[suggestion, about];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == [tableView numberOfSections] - 1) {
        return 44.0;
    }
    else {
        return tableView.sectionFooterHeight;
    }
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == [tableView numberOfSections] - 1) {
        return self.logoutView;
    }
    else {
        return nil;
    }
}


@end
